<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-OyDaGkZ9eq">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="#本机课程目标  掌握for循环语句的基本语法结构 掌握while和until循环语句的基本语法结构">
<meta property="og:type" content="article">
<meta property="og:title" content="04_SHELL编程之循环语句">
<meta property="og:url" content="http://example.com/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/04_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/index.html">
<meta property="og:site_name" content="极风键客">
<meta property="og:description" content="#本机课程目标  掌握for循环语句的基本语法结构 掌握while和until循环语句的基本语法结构">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-04T12:46:25.000Z">
<meta property="article:modified_time" content="2021-02-17T07:38:03.090Z">
<meta property="article:author" content="王增明">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/04_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>04_SHELL编程之循环语句 | 极风键客</title>
  


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b94f963e94e4127fa71ab49cbddce93b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="极风键客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">极风键客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">技术狂热爱好者!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbmd6ZW5nbWluZw==" title="客官,来嘛~" aria-label="客官,来嘛~"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/04_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="王增明">
      <meta itemprop="description" content="喜欢折腾的极客聚集地.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="极风键客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          04_SHELL编程之循环语句
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-01-04 20:46:25" itemprop="dateCreated datePublished" datetime="2021-01-04T20:46:25+08:00">2021-01-04</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-02-17 15:38:03" itemprop="dateModified" datetime="2021-02-17T15:38:03+08:00">2021-02-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>8.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>8 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>#本机课程目标</p>
<ul>
<li>掌握for循环语句的基本语法结构</li>
<li>掌握while和until循环语句的基本语法结构</li>
</ul>
<a id="more"></a>

<h1 id="一、-for循环语句"><a href="#一、-for循环语句" class="headerlink" title="一、==for循环语句=="></a>一、==for循环语句==</h1><p><strong>关键词：爱的魔力转圈圈</strong>:innocent:</p>
<h2 id="1-for循环语法结构"><a href="#1-for循环语法结构" class="headerlink" title="1. for循环语法结构"></a>1. for循环语法结构</h2><h3 id="㈠-列表-循环"><a href="#㈠-列表-循环" class="headerlink" title="㈠ ==列表==循环"></a>㈠ ==列表==循环</h3><blockquote>
<p>列表for循环：用于将一组命令执行**==已知的次数==**</p>
</blockquote>
<ul>
<li><strong>基本语法格式</strong></li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> variable <span class="keyword">in</span> &#123;list&#125;</span><br><span class="line">     <span class="keyword">do</span></span><br><span class="line">          command </span><br><span class="line">          command</span><br><span class="line">          …</span><br><span class="line">     done</span><br><span class="line">或者</span><br><span class="line"><span class="keyword">for</span> variable <span class="keyword">in</span> a b c</span><br><span class="line">     <span class="keyword">do</span></span><br><span class="line">         command</span><br><span class="line">         command</span><br><span class="line">     done</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>举例说明</strong></li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># for var in &#123;1..10&#125;;do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in 1 2 3 4 5;do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in `seq 10`;do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in $(seq 10);do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in &#123;0..10..2&#125;;do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in &#123;2..10..2&#125;;do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in &#123;10..1&#125;;do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in &#123;10..1..-2&#125;;do echo $var;done</span></span><br><span class="line"><span class="comment"># for var in `seq 10 -2 1`;do echo $var;done</span></span><br></pre></td></tr></table></figure>
<h3 id="㈡-不带列表循环"><a href="#㈡-不带列表循环" class="headerlink" title="㈡ 不带列表循环"></a>㈡ 不带列表循环</h3><blockquote>
<p> 不带列表的for循环执行时由<strong>用户指定参数和参数的个数</strong></p>
</blockquote>
<ul>
<li><strong>基本语法格式</strong></li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> variable</span><br><span class="line">    <span class="keyword">do</span></span><br><span class="line">        command </span><br><span class="line">        command</span><br><span class="line">        …</span><br><span class="line">   done</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>举例说明</strong></li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> var</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$var</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;脚本后面有<span class="variable">$</span>#个参数&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="㈢-类C风格的for循环"><a href="#㈢-类C风格的for循环" class="headerlink" title="㈢ 类C风格的for循环"></a>㈢ 类C风格的for循环</h3><ul>
<li><strong>基本语法结构</strong></li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(( expr1;expr2;expr3 ))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">		command</span><br><span class="line">		command</span><br><span class="line">		…</span><br><span class="line">	done</span><br><span class="line"><span class="keyword">for</span> (( i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line">	done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">expr1：定义变量并赋初值</span><br><span class="line">expr2：决定是否进行循环（条件）</span><br><span class="line">expr3：决定循环变量如何改变，决定循环什么时候退出</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>举例说明</strong></li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># for ((i=1;i&lt;=5;i++));do echo $i;done</span></span><br><span class="line"><span class="comment"># for ((i=1;i&lt;=10;i+=2));do echo $i;done</span></span><br><span class="line"><span class="comment"># for ((i=2;i&lt;=10;i+=2));do echo $i;done</span></span><br></pre></td></tr></table></figure>
<h2 id="2-应用案例"><a href="#2-应用案例" class="headerlink" title="2. 应用案例"></a>2. 应用案例</h2><h3 id="㈠-脚本-计算-1-100奇数和"><a href="#㈠-脚本-计算-1-100奇数和" class="headerlink" title="㈠ 脚本==计算==1-100奇数和"></a>㈠ 脚本==计算==1-100奇数和</h3><h4 id="①-思路"><a href="#①-思路" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>定义一个变量来保存奇数的和   ==sum===0</li>
<li>找出1-100的奇数，保存到另一个变量里  ==i===遍历出来的奇数</li>
<li>从1-100中找出奇数后，再相加，然后将和赋值给变量  循环变量  for</li>
<li>遍历完毕后，将sum的值打印出来</li>
</ol>
<h4 id="②-落地实现（条条大路通罗马）"><a href="#②-落地实现（条条大路通罗马）" class="headerlink" title="② 落地实现（条条大路通罗马）"></a>② 落地实现（条条大路通罗马）</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment"># 计算1-100的奇数和</span></span><br><span class="line"><span class="comment"># 定义变量来保存奇数和</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#for循环遍历1-100的奇数，并且相加，把结果重新赋值给sum</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> &#123;<span class="number">1</span>..<span class="number">100</span>..<span class="number">2</span>&#125;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	let sum=<span class="variable">$sum</span>+<span class="variable">$i</span></span><br><span class="line">done</span><br><span class="line"><span class="comment">#打印所有奇数的和</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-100的奇数和是:<span class="variable">$sum</span>&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">方法<span class="number">1</span>：</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> &#123;<span class="number">1</span>..<span class="number">100</span>..<span class="number">2</span>&#125;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	sum=<span class="variable">$</span>[<span class="variable">$i</span>+<span class="variable">$sum</span>]</span><br><span class="line">done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-100的奇数和为:<span class="variable">$sum</span>&quot;</span></span><br><span class="line"></span><br><span class="line">方法<span class="number">2</span>：</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">100</span>;i+=<span class="number">2</span>))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	let sum=<span class="variable">$i</span>+<span class="variable">$sum</span></span><br><span class="line">done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-100的奇数和为:<span class="variable">$sum</span>&quot;</span></span><br><span class="line"></span><br><span class="line">方法<span class="number">3</span>：</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">100</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$</span>[<span class="variable">$i</span>%<span class="number">2</span>] -<span class="type">ne</span> <span class="number">0</span> ];then</span><br><span class="line">	let sum=<span class="variable">$sum</span>+<span class="variable">$i</span></span><br><span class="line">	fi</span><br><span class="line">或者</span><br><span class="line">test <span class="variable">$</span>[<span class="variable">$i</span>%<span class="number">2</span>] <span class="operator">-ne</span> <span class="number">0</span> &amp;&amp; let sum=<span class="variable">$sum</span>+<span class="variable">$i</span></span><br><span class="line"></span><br><span class="line">done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-100的奇数和为:<span class="variable">$sum</span>&quot;</span></span><br><span class="line"></span><br><span class="line">方法<span class="number">4</span>：</span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">100</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$</span>[<span class="variable">$i</span>%<span class="number">2</span>] -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">	<span class="keyword">continue</span></span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	let sum=<span class="variable">$sum</span>+<span class="variable">$i</span></span><br><span class="line">	fi</span><br><span class="line">done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-100的奇数和为:<span class="variable">$sum</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">100</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	test <span class="variable">$</span>[<span class="variable">$i</span>%<span class="number">2</span>] <span class="operator">-eq</span> <span class="number">0</span> &amp;&amp; <span class="keyword">continue</span> || let sum=sum+<span class="variable">$i</span></span><br><span class="line">done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-100的奇数和是:<span class="variable">$sum</span>&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="③-循环控制语句"><a href="#③-循环控制语句" class="headerlink" title="③ 循环控制语句"></a>③ 循环控制语句</h4><p><strong>循环体：</strong> ==do….done==之间的内容</p>
<ul>
<li>continue：继续；表示==循环体==内下面的代码不执行，重新开始下一次循环</li>
<li>break：打断；马上停止执行本次循环，执行==循环体==后面的代码</li>
<li>exit：表示直接跳出程序</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[root@server ~]# cat for5.sh </span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line">for i in &#123;1..5&#125;</span><br><span class="line">do</span><br><span class="line">	test $i -eq 2 &amp;&amp; break || touch /tmp/file$i</span><br><span class="line">done</span><br><span class="line">echo hello hahahah</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈡-判断所输整数是否为质数"><a href="#㈡-判断所输整数是否为质数" class="headerlink" title="㈡ 判断所输整数是否为质数"></a>㈡ 判断所输整数是否为质数</h3><p><strong>质数(素数)：</strong>==只能==被1和它本身==整除==的数叫质数。<br>2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</p>
<h4 id="①-思路-1"><a href="#①-思路-1" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>让用户输入一个数，保存到一个变量里   <code>read -p &quot;请输入一个正整数:&quot; num</code></li>
<li>如果能被其他数整除就不是质数——&gt;<code>$num%$i </code>是否等于0    <code>$i=2到$num-1</code></li>
<li>如果输入的数是1或者2取模根据上面判断又不符合，所以先排除1和2</li>
<li>测试序列从2开始，输入的数是4——&gt;得出结果<code>$num</code>不能和<code>$i</code>相等，并且<code>$num</code>不能小于<code>$i</code></li>
</ol>
<h4 id="②-落地实现"><a href="#②-落地实现" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment">#定义变量来保存用户所输入数字</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入一个正整数字:&quot;</span> number</span><br><span class="line"></span><br><span class="line"><span class="comment">#先排除用户输入的数字1和2</span></span><br><span class="line">[ <span class="variable">$number</span> -<span class="type">eq</span> <span class="number">1</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$number</span>不是质数&quot;</span> &amp;&amp; <span class="keyword">exit</span></span><br><span class="line">[ <span class="variable">$number</span> -<span class="type">eq</span> <span class="number">2</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$number</span>是质数&quot;</span> &amp;&amp; <span class="keyword">exit</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#循环判断用户所输入的数字是否质数</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq <span class="number">2</span> <span class="variable">$</span>[<span class="variable">$number</span>-<span class="number">1</span>]`</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	 [ <span class="variable">$</span>[<span class="variable">$number</span>%<span class="variable">$i</span>] -<span class="type">eq</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$number</span>不是质数&quot;</span> &amp;&amp; <span class="keyword">exit</span></span><br><span class="line">	done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$number</span>是质数&quot;</span></span><br><span class="line"></span><br><span class="line">优化思路：没有必要全部产生<span class="number">2</span>~<span class="variable">$</span>[<span class="variable">$number</span>-<span class="number">1</span>]序列，只需要产生一半即可。</span><br><span class="line"></span><br><span class="line">更好解决办法：类C风格完美避开了生成序列的坑</span><br><span class="line"><span class="keyword">for</span> (( i=<span class="number">2</span>;i&lt;=<span class="variable">$</span>[<span class="variable">$number</span>-<span class="number">1</span>];i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        [ <span class="variable">$</span>[<span class="variable">$number</span>%<span class="variable">$i</span>] -<span class="type">eq</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$number</span>不是质数&quot;</span> &amp;&amp; <span class="keyword">exit</span></span><br><span class="line"></span><br><span class="line">done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$number</span>是质数&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="㈢-批量创建用户"><a href="#㈢-批量创建用户" class="headerlink" title="㈢ 批量创建用户"></a>㈢ 批量创建用户</h3><p><strong>需求：</strong>批量加5个新用户，以u1到u5命名，并统一加一个新组，组名为class,统一改密码为123</p>
<h4 id="①-思路-2"><a href="#①-思路-2" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>添加用户的命令    <code>useradd -G class</code></li>
<li>判断class组是否存在  <code>grep -w ^class /etc/group</code> 或者<code>groupadd class</code></li>
<li>根据题意，判断该脚本循环5次来添加用户  <code>for</code></li>
<li>给用户设置密码，应该放到循环体里面</li>
</ol>
<h4 id="②-落地实现-1"><a href="#②-落地实现-1" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment">#判断class组是否存在</span></span><br><span class="line">grep <span class="literal">-w</span> ^<span class="class"><span class="keyword">class</span> /<span class="title">etc</span>/<span class="title">group</span> &amp;&gt;/<span class="title">dev</span>/<span class="title">null</span></span></span><br><span class="line"><span class="class"><span class="title">test</span> $? -<span class="title">ne</span> 0 &amp;&amp; <span class="title">groupadd</span> <span class="title">class</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">#循环创建用户</span></span><br><span class="line"><span class="class"><span class="title">for</span> ((<span class="title">i</span>=1;<span class="title">i</span>&lt;=5;<span class="title">i</span>++))</span></span><br><span class="line"><span class="class"><span class="title">do</span></span></span><br><span class="line"><span class="class">	<span class="title">useradd</span> -<span class="title">G</span> <span class="title">class</span> <span class="title">u</span>$<span class="title">i</span></span></span><br><span class="line"><span class="class">	<span class="title">echo</span> 123|<span class="title">passwd</span> --<span class="title">stdin</span> <span class="title">u</span>$<span class="title">i</span></span></span><br><span class="line"><span class="class"><span class="title">done</span></span></span><br><span class="line"><span class="class">#用户创建信息保存日志文件</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">方法一：</span></span><br><span class="line"><span class="class">#!/<span class="title">bin</span>/<span class="title">bash</span></span></span><br><span class="line"><span class="class">#判断<span class="title">class</span>组是否存在</span></span><br><span class="line"><span class="class"><span class="title">grep</span> -<span class="title">w</span> <span class="title">class</span> /<span class="title">etc</span>/<span class="title">group</span> &amp;&gt;/<span class="title">dev</span>/<span class="title">null</span></span></span><br><span class="line"><span class="class">[ $? -<span class="title">ne</span> 0 ] &amp;&amp; <span class="title">groupadd</span> <span class="title">class</span></span></span><br><span class="line"><span class="class">#批量创建5个用户</span></span><br><span class="line"><span class="class"><span class="title">for</span> <span class="title">i</span> <span class="title">in</span></span> &#123;<span class="number">1</span>..<span class="number">5</span>&#125;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	useradd <span class="literal">-G</span> <span class="class"><span class="keyword">class</span> <span class="title">u</span>$<span class="title">i</span></span></span><br><span class="line"><span class="class">	<span class="title">echo</span> 123|<span class="title">passwd</span> --<span class="title">stdin</span> <span class="title">u</span>$<span class="title">i</span></span></span><br><span class="line"><span class="class"><span class="title">done</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">方法二：</span></span><br><span class="line"><span class="class">#!/<span class="title">bin</span>/<span class="title">bash</span></span></span><br><span class="line"><span class="class">#判断<span class="title">class</span>组是否存在</span></span><br><span class="line"><span class="class"><span class="title">cut</span> -<span class="title">d</span>: -<span class="title">f1</span> /<span class="title">etc</span>/<span class="title">group</span>|<span class="title">grep</span> -<span class="title">w</span> <span class="title">class</span> &amp;&gt;/<span class="title">dev</span>/<span class="title">null</span></span></span><br><span class="line"><span class="class">[ $? -<span class="title">ne</span> 0 ] &amp;&amp; <span class="title">groupadd</span> <span class="title">class</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">#循环增加用户，循环次数5次，<span class="title">for</span>循环,给用户设定密码</span></span><br><span class="line"><span class="class"><span class="title">for</span> ((<span class="title">i</span>=1;<span class="title">i</span>&lt;=5;<span class="title">i</span>++))</span></span><br><span class="line"><span class="class"><span class="title">do</span></span></span><br><span class="line"><span class="class">	<span class="title">useradd</span> <span class="title">u</span>$<span class="title">i</span> -<span class="title">G</span> <span class="title">class</span></span></span><br><span class="line"><span class="class">	<span class="title">echo</span> 123|<span class="title">passwd</span> --<span class="title">stdin</span> <span class="title">u</span>$<span class="title">i</span></span></span><br><span class="line"><span class="class"><span class="title">done</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">方法三：</span></span><br><span class="line"><span class="class">#!/<span class="title">bin</span>/<span class="title">bash</span></span></span><br><span class="line"><span class="class"><span class="title">grep</span> -<span class="title">w</span> <span class="title">class</span> /<span class="title">etc</span>/<span class="title">group</span> &amp;&gt;/<span class="title">dev</span>/<span class="title">null</span></span></span><br><span class="line"><span class="class"><span class="title">test</span> $? -<span class="title">ne</span> 0 &amp;&amp; <span class="title">groupadd</span> <span class="title">class</span></span></span><br><span class="line"><span class="class">或者</span></span><br><span class="line"><span class="class"><span class="title">groupadd</span> <span class="title">class</span> &amp;&gt;/<span class="title">dev</span>/<span class="title">null</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">for</span> ((<span class="title">i</span>=1;<span class="title">i</span>&lt;=5;<span class="title">i</span>++))</span></span><br><span class="line"><span class="class"><span class="title">do</span></span></span><br><span class="line"><span class="class"><span class="title">useradd</span> -<span class="title">G</span> <span class="title">class</span> <span class="title">u</span>$<span class="title">i</span> &amp;&amp; <span class="title">echo</span> 123|<span class="title">passwd</span> --<span class="title">stdin</span> <span class="title">u</span>$<span class="title">i</span></span></span><br><span class="line"><span class="class"><span class="title">done</span></span></span><br><span class="line"><span class="class"></span></span><br></pre></td></tr></table></figure>
<h2 id="3-课堂练习"><a href="#3-课堂练习" class="headerlink" title="3. 课堂练习"></a>3. 课堂练习</h2><h3 id="㈠-批量创建用户"><a href="#㈠-批量创建用户" class="headerlink" title="㈠ 批量创建用户"></a>㈠ 批量创建用户</h3><p>**需求1:**批量新建5个用户stu1~stu5，要求这几个用户的家目录都在/rhome.</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#判断/rhome是否存在</span></span><br><span class="line">[ -<span class="type">f</span> /<span class="type">rhome</span> ] &amp;&amp; <span class="built_in">mv</span> /rhome /rhome.bak</span><br><span class="line">test ! <span class="literal">-d</span> /rhome &amp;&amp; mkdir /rhome</span><br><span class="line">或者</span><br><span class="line">[ -<span class="type">f</span> /<span class="type">rhome</span> ] &amp;&amp; <span class="built_in">mv</span> /rhome /rhome.bak || [ ! -<span class="type">d</span> /<span class="type">rhome</span> ] &amp;&amp; mkdir /rhome </span><br><span class="line"></span><br><span class="line"><span class="comment">#创建用户，循环5次</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	useradd <span class="literal">-d</span> /rhome/stu<span class="variable">$i</span> stu<span class="variable">$i</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="number">123</span>|passwd -<span class="literal">-stdin</span> stu<span class="variable">$i</span></span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="㈡-局域网内脚本检查主机网络通讯"><a href="#㈡-局域网内脚本检查主机网络通讯" class="headerlink" title="㈡ 局域网内脚本检查主机网络通讯"></a>㈡ 局域网内脚本检查主机网络通讯</h3><p><strong>需求2：</strong></p>
<p>写一个脚本，局域网内，把能ping通的IP和不能ping通的IP分类，并保存到两个文本文件里</p>
<p>以10.1.1.1~10.1.1.10为例</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span>~<span class="number">10.1</span>.<span class="number">1.254</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">ip=<span class="number">10.1</span>.<span class="number">1</span></span><br><span class="line"><span class="comment">#循环去ping主机的IP</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">10</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	ping <span class="literal">-c1</span> <span class="variable">$ip</span>.<span class="variable">$i</span> &amp;&gt;/dev/null</span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">		<span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ip</span>.<span class="variable">$i</span> is ok&quot;</span> &gt;&gt; /tmp/ip_up.txt</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ip</span>.<span class="variable">$i</span> is down&quot;</span> &gt;&gt; /tmp/ip_down.txt</span><br><span class="line">	fi</span><br><span class="line">	或者</span><br><span class="line">	[ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ip</span>.<span class="variable">$i</span> is ok&quot;</span> &gt;&gt; /tmp/ip_up.txt || <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ip</span>.<span class="variable">$i</span> is down&quot;</span> &gt;&gt; /tmp/ip_down.txt</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell03</span>]<span class="comment"># time ./ping.sh         </span></span><br><span class="line"></span><br><span class="line">real    <span class="number">0</span>m24.<span class="number">129</span>s</span><br><span class="line">user    <span class="number">0</span>m0.<span class="number">006</span>s</span><br><span class="line">sys     <span class="number">0</span>m0.<span class="number">005</span>s</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>延伸扩展：shell脚本并发</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">并行执行：</span><br><span class="line">&#123;程序&#125;&amp;表示将程序放到后台并行执行，如果需要等待程序执行完毕再进行下面内容，需要加wait</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">ip=<span class="number">10.1</span>.<span class="number">1</span></span><br><span class="line"><span class="comment">#循环去ping主机的IP</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">10</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">        ping <span class="literal">-c1</span> <span class="variable">$ip</span>.<span class="variable">$i</span> &amp;&gt;/dev/null</span><br><span class="line">        <span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">                <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ip</span>.<span class="variable">$i</span> is ok&quot;</span> &gt;&gt; /tmp/ip_up.txt</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">                <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ip</span>.<span class="variable">$i</span> is down&quot;</span> &gt;&gt; /tmp/ip_down.txt</span><br><span class="line">        fi</span><br><span class="line">&#125;&amp;</span><br><span class="line">done</span><br><span class="line">wait</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;ip is ok....&quot;</span></span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># time ./ping.sh </span></span><br><span class="line">ip is ok...</span><br><span class="line"></span><br><span class="line">real    <span class="number">0</span>m3.<span class="number">091</span>s</span><br><span class="line">user    <span class="number">0</span>m0.<span class="number">001</span>s</span><br><span class="line">sys     <span class="number">0</span>m0.<span class="number">008</span>s</span><br></pre></td></tr></table></figure>
<h3 id="㈢-判断闰年"><a href="#㈢-判断闰年" class="headerlink" title="㈢ 判断闰年"></a>㈢ 判断闰年</h3><p><strong>需求3：</strong></p>
<p>输入一个年份，判断是否是润年（能被4整除但不能被100整除，或能被400整除的年份即为闰年）</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;Please input year:(2017)&quot;</span> year</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$</span>[<span class="variable">$year</span>%<span class="number">4</span>] -<span class="type">eq</span> <span class="number">0</span> -<span class="type">a</span> <span class="variable">$</span>[<span class="variable">$year</span>%<span class="number">100</span>] -<span class="type">ne</span> <span class="number">0</span> ];then</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$year</span> is leap year&quot;</span></span><br><span class="line">elif [ <span class="variable">$</span>[<span class="variable">$year</span>%<span class="number">400</span>] -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$year</span> is leap year&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$year</span> is not leap year&quot;</span></span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
<p>##4. 总结</p>
<ul>
<li>FOR循环语法结构</li>
<li>FOR循环可以结合==条件判断和流程控制语句==<ul>
<li>do ……done  循环体</li>
<li>循环体里可以是命令集合，再加上条件判断以及流程控制</li>
</ul>
</li>
<li>控制循环语句<ul>
<li>continue  继续，跳过本次循环，继续下一次循环</li>
<li>break       打断，跳出循环，==执行==循环体外的代码</li>
<li>exit          退出，直接退出程序</li>
</ul>
</li>
</ul>
<p>#二、**==while循环语句==**</p>
<p><strong>特点：</strong>==条件为真就进入循环；条件为假就退出循环==</p>
<p>##1. while循环语法结构</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> 表达式</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">		command...</span><br><span class="line">	done</span><br><span class="line">	</span><br><span class="line"><span class="keyword">while</span>  [ <span class="number">1</span> -<span class="type">eq</span> <span class="number">1</span> ] 或者 (( <span class="number">1</span> &gt; <span class="number">2</span> ))</span><br><span class="line">  <span class="keyword">do</span></span><br><span class="line">     command</span><br><span class="line">     command</span><br><span class="line">     ...</span><br><span class="line"> done</span><br></pre></td></tr></table></figure>
<p><strong>循环打印1-5数字</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FOR</span>循环打印：</span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>循环打印：</span><br><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$i</span> -<span class="type">le</span> <span class="number">5</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line">	let i++</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h2 id="2-应用案例-1"><a href="#2-应用案例-1" class="headerlink" title="2. 应用案例"></a>2. 应用案例</h2><h3 id="㈠-脚本计算1-50偶数和"><a href="#㈠-脚本计算1-50偶数和" class="headerlink" title="㈠ 脚本计算1-50偶数和"></a>㈠ 脚本计算1-50偶数和</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">0</span>;i&lt;=<span class="number">50</span>;i+=<span class="number">2</span>))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	let sum=<span class="variable">$sum</span>+<span class="variable">$i</span>  (let sum=sum+i)</span><br><span class="line">done</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-50的偶数和为:<span class="variable">$sum</span>&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line">i=<span class="number">2</span></span><br><span class="line"><span class="comment">#循环打印1-50的偶数和并且计算后重新赋值给sum</span></span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$i</span> -<span class="type">le</span> <span class="number">50</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	let sum=<span class="variable">$sum</span>+<span class="variable">$i</span></span><br><span class="line">	let i+=<span class="number">2</span>  或者 <span class="variable">$</span>[<span class="variable">$i</span>+<span class="number">2</span>]</span><br><span class="line">done</span><br><span class="line"><span class="comment">#打印sum的值</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;1-50的偶数和为:<span class="variable">$sum</span>&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="㈡-脚本同步系统时间"><a href="#㈡-脚本同步系统时间" class="headerlink" title="㈡ 脚本同步系统时间"></a>㈡ 脚本同步系统时间</h3><h4 id="①-具体需求"><a href="#①-具体需求" class="headerlink" title="① 具体需求"></a>① 具体需求</h4><ol>
<li>写一个脚本，==30秒==同步一次系统时间，时间同步服务器10.1.1.1</li>
<li>如果同步失败，则进行邮件报警,每次失败都报警</li>
<li>如果同步成功,也进行邮件通知,但是==成功100次==才通知一次</li>
</ol>
<h4 id="②-思路"><a href="#②-思路" class="headerlink" title="② 思路"></a>② 思路</h4><ol>
<li><p>每隔30s同步一次时间，该脚本是一个死循环   while 循环</p>
</li>
<li><p>同步失败发送邮件   1) ntpdate 10.1.1.1  2) rdate -s 10.1.1.1</p>
</li>
<li><p>同步成功100次发送邮件   定义变量保存成功次数 </p>
</li>
</ol>
<h4 id="③-落地实现"><a href="#③-落地实现" class="headerlink" title="③ 落地实现"></a>③ 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment"># 该脚本用于时间同步</span></span><br><span class="line">NTP=<span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line">count=<span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> true</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	ntpdate <span class="variable">$NTP</span> &amp;&gt;/dev/null</span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">ne</span> <span class="number">0</span> ];then</span><br><span class="line">		<span class="built_in">echo</span> <span class="string">&quot;system date failed&quot;</span> |mail <span class="literal">-s</span> <span class="string">&quot;check system date&quot;</span>  root@localhost</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		let count++</span><br><span class="line">		<span class="keyword">if</span> [ <span class="variable">$count</span> -<span class="type">eq</span> <span class="number">100</span> ];then</span><br><span class="line">		<span class="built_in">echo</span> <span class="string">&quot;systemc date success&quot;</span> |mail <span class="literal">-s</span> <span class="string">&quot;check system date&quot;</span>  root@localhost &amp;&amp; count=<span class="number">0</span></span><br><span class="line">		fi</span><br><span class="line">	fi</span><br><span class="line"><span class="built_in">sleep</span> <span class="number">30</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">count=<span class="number">0</span></span><br><span class="line">ntp_server=<span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="keyword">while</span> true</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	rdate <span class="literal">-s</span> <span class="variable">$ntp</span><span class="literal">-server</span> &amp;&gt;/dev/null</span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">ne</span> <span class="number">0</span> ];then</span><br><span class="line">		<span class="built_in">echo</span> <span class="string">&quot;system date failed&quot;</span> |mail <span class="literal">-s</span> <span class="string">&#x27;check system date&#x27;</span>  root@localhost	</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		let count++</span><br><span class="line">		<span class="keyword">if</span> [ <span class="variable">$</span>[<span class="variable">$count</span>%<span class="number">100</span>] -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">		<span class="built_in">echo</span> <span class="string">&quot;system date successfull&quot;</span> |mail <span class="literal">-s</span> <span class="string">&#x27;check system date&#x27;</span>  root@localhost &amp;&amp; count=<span class="number">0</span></span><br><span class="line">		fi</span><br><span class="line">	fi</span><br><span class="line"><span class="built_in">sleep</span> <span class="number">3</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">以上脚本还有更多的写法，课后自己完成</span><br></pre></td></tr></table></figure>
<p>#三、until循环</p>
<p><strong>特点</strong>：==条件为假就进入循环；条件为真就退出循环==</p>
<h2 id="1-until语法结构"><a href="#1-until语法结构" class="headerlink" title="1. until语法结构"></a>1. until语法结构</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">until expression   [ 1 -eq 1 ]  (( 1 &gt;= 1 ))</span><br><span class="line">	do</span><br><span class="line">		command</span><br><span class="line">		command</span><br><span class="line">		...</span><br><span class="line">	done</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>打印1-5数字</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$i</span> -<span class="type">le</span> <span class="number">5</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line">	let i++</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">until</span> [ <span class="variable">$i</span> -<span class="type">gt</span> <span class="number">5</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line">	let i++</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h2 id="2-应用案例-2"><a href="#2-应用案例-2" class="headerlink" title="2. 应用案例"></a>2. 应用案例</h2><p>###㈠ 具体需求</p>
<ol>
<li>使用until语句批量创建10个用户，要求stu1—stu5用户的UID分别为1001—1005；</li>
<li>stu6~stu10用户的家目录分别在/rhome/stu6—/rhome/stu10</li>
</ol>
<h3 id="㈡-思路"><a href="#㈡-思路" class="headerlink" title="㈡ 思路"></a>㈡ 思路</h3><ol>
<li>创建用户语句  <code>useradd -u|useradd -d</code></li>
<li>使用循环语句(until)批量创建用户  <code>until循环语句结构</code></li>
<li>判断用户前5个和后5个  <code>条件判断语句</code></li>
</ol>
<h3 id="㈢-落地实现"><a href="#㈢-落地实现" class="headerlink" title="㈢ 落地实现"></a>㈢ 落地实现</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#！/bin/env bash</span></span><br><span class="line"><span class="keyword">if</span> [ -<span class="type">d</span> /<span class="type">rhome</span> ];then</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;/rhome目录已存在&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    mkdir /rhome</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;/rhome不存在，已完成创建&quot;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">until</span> [ <span class="variable">$i</span> -<span class="type">gt</span> <span class="number">10</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        <span class="keyword">if</span> [ <span class="variable">$i</span> -<span class="type">le</span> <span class="number">5</span> ];then</span><br><span class="line">                useradd <span class="literal">-u</span> <span class="variable">$</span>[<span class="number">1000</span>+<span class="variable">$i</span>] stu<span class="variable">$i</span></span><br><span class="line">                <span class="built_in">echo</span> <span class="number">123</span>|passwd -<span class="literal">-stdin</span> stu<span class="variable">$i</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">                useradd <span class="literal">-d</span> /rhome/stu<span class="variable">$i</span> stu<span class="variable">$i</span></span><br><span class="line">                <span class="built_in">echo</span> <span class="number">123</span>|passwd -<span class="literal">-stdin</span> stu<span class="variable">$i</span></span><br><span class="line">        fi</span><br><span class="line">let i++</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">==================================================</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">until</span> [ <span class="variable">$i</span> -<span class="type">gt</span> <span class="number">10</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$i</span> -<span class="type">le</span> <span class="number">5</span> ];then</span><br><span class="line">		useradd <span class="literal">-u</span> <span class="variable">$</span>[<span class="number">1000</span>+<span class="variable">$i</span>] stu<span class="variable">$i</span> &amp;&amp; <span class="built_in">echo</span> <span class="number">123</span>|passwd -<span class="literal">-stdin</span> stu<span class="variable">$i</span></span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		[ ! -<span class="type">d</span> /<span class="type">rhome</span> ] &amp;&amp; mkdir /rhome</span><br><span class="line">		useradd <span class="literal">-d</span> /rhome/stu<span class="variable">$i</span> stu<span class="variable">$i</span> &amp;&amp; <span class="built_in">echo</span> <span class="number">123</span>|passwd -<span class="literal">-stdin</span> stu<span class="variable">$i</span>		</span><br><span class="line">	fi</span><br><span class="line">let i++</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h1 id="四、课后作业"><a href="#四、课后作业" class="headerlink" title="四、课后作业"></a>四、课后作业</h1><ol>
<li>判断/tmp/run目录是否存在，如果不存在就建立，如果存在就删除目录里所有文件</li>
<li> 输入一个路径，判断路径是否存在，而且输出是文件还是目录，如果是链接文件，还得输出是  有效的连接还是无效的连接 </li>
<li>交互模式要求输入一个ip，然后脚本判断这个IP 对应的主机是否 能ping 通，输出结果类似于：<br>Server  10.1.1.20 is Down! 最后要求把结果邮件到本地管理员root@localhost mail01@localhost</li>
<li>写一个脚本/home/program，要求当给脚本输入参数hello时，脚本返回world,给脚本输入参数world时，脚本返回hello。而脚本没有参数或者参数错误时，屏幕上输出“usage:/home/program hello or world”</li>
<li>写一个脚本自动搭建nfs服务</li>
</ol>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/linux/" rel="tag"># linux</a>
              <a href="/tags/shell/" rel="tag"># shell</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch%E6%96%87%E6%A1%A3/" rel="prev" title="ElasticSearch 使用">
      <i class="fa fa-chevron-left"></i> ElasticSearch 使用
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/01/05/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/05_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF+%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%8F%8A%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B/" rel="next" title="05_SHELL编程之嵌套循环+随机数及综合案例">
      05_SHELL编程之嵌套循环+随机数及综合案例 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81-for%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">1.</span> <span class="nav-text">一、&#x3D;&#x3D;for循环语句&#x3D;&#x3D;</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-for%E5%BE%AA%E7%8E%AF%E8%AF%AD%E6%B3%95%E7%BB%93%E6%9E%84"><span class="nav-number">1.1.</span> <span class="nav-text">1. for循环语法结构</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E5%88%97%E8%A1%A8-%E5%BE%AA%E7%8E%AF"><span class="nav-number">1.1.1.</span> <span class="nav-text">㈠ &#x3D;&#x3D;列表&#x3D;&#x3D;循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E4%B8%8D%E5%B8%A6%E5%88%97%E8%A1%A8%E5%BE%AA%E7%8E%AF"><span class="nav-number">1.1.2.</span> <span class="nav-text">㈡ 不带列表循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E7%B1%BBC%E9%A3%8E%E6%A0%BC%E7%9A%84for%E5%BE%AA%E7%8E%AF"><span class="nav-number">1.1.3.</span> <span class="nav-text">㈢ 类C风格的for循环</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B"><span class="nav-number">1.2.</span> <span class="nav-text">2. 应用案例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E8%84%9A%E6%9C%AC-%E8%AE%A1%E7%AE%97-1-100%E5%A5%87%E6%95%B0%E5%92%8C"><span class="nav-number">1.2.1.</span> <span class="nav-text">㈠ 脚本&#x3D;&#x3D;计算&#x3D;&#x3D;1-100奇数和</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF"><span class="nav-number">1.2.1.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0%EF%BC%88%E6%9D%A1%E6%9D%A1%E5%A4%A7%E8%B7%AF%E9%80%9A%E7%BD%97%E9%A9%AC%EF%BC%89"><span class="nav-number">1.2.1.2.</span> <span class="nav-text">② 落地实现（条条大路通罗马）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A2-%E5%BE%AA%E7%8E%AF%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="nav-number">1.2.1.3.</span> <span class="nav-text">③ 循环控制语句</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E5%88%A4%E6%96%AD%E6%89%80%E8%BE%93%E6%95%B4%E6%95%B0%E6%98%AF%E5%90%A6%E4%B8%BA%E8%B4%A8%E6%95%B0"><span class="nav-number">1.2.2.</span> <span class="nav-text">㈡ 判断所输整数是否为质数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF-1"><span class="nav-number">1.2.2.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.2.2.2.</span> <span class="nav-text">② 落地实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E6%89%B9%E9%87%8F%E5%88%9B%E5%BB%BA%E7%94%A8%E6%88%B7"><span class="nav-number">1.2.3.</span> <span class="nav-text">㈢ 批量创建用户</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF-2"><span class="nav-number">1.2.3.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0-1"><span class="nav-number">1.2.3.2.</span> <span class="nav-text">② 落地实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E8%AF%BE%E5%A0%82%E7%BB%83%E4%B9%A0"><span class="nav-number">1.3.</span> <span class="nav-text">3. 课堂练习</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E6%89%B9%E9%87%8F%E5%88%9B%E5%BB%BA%E7%94%A8%E6%88%B7"><span class="nav-number">1.3.1.</span> <span class="nav-text">㈠ 批量创建用户</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E5%B1%80%E5%9F%9F%E7%BD%91%E5%86%85%E8%84%9A%E6%9C%AC%E6%A3%80%E6%9F%A5%E4%B8%BB%E6%9C%BA%E7%BD%91%E7%BB%9C%E9%80%9A%E8%AE%AF"><span class="nav-number">1.3.2.</span> <span class="nav-text">㈡ 局域网内脚本检查主机网络通讯</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E5%88%A4%E6%96%AD%E9%97%B0%E5%B9%B4"><span class="nav-number">1.3.3.</span> <span class="nav-text">㈢ 判断闰年</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B-1"><span class="nav-number">1.4.</span> <span class="nav-text">2. 应用案例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E8%84%9A%E6%9C%AC%E8%AE%A1%E7%AE%971-50%E5%81%B6%E6%95%B0%E5%92%8C"><span class="nav-number">1.4.1.</span> <span class="nav-text">㈠ 脚本计算1-50偶数和</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E8%84%9A%E6%9C%AC%E5%90%8C%E6%AD%A5%E7%B3%BB%E7%BB%9F%E6%97%B6%E9%97%B4"><span class="nav-number">1.4.2.</span> <span class="nav-text">㈡ 脚本同步系统时间</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E5%85%B7%E4%BD%93%E9%9C%80%E6%B1%82"><span class="nav-number">1.4.2.1.</span> <span class="nav-text">① 具体需求</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E6%80%9D%E8%B7%AF"><span class="nav-number">1.4.2.2.</span> <span class="nav-text">② 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A2-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.4.2.3.</span> <span class="nav-text">③ 落地实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-until%E8%AF%AD%E6%B3%95%E7%BB%93%E6%9E%84"><span class="nav-number">1.5.</span> <span class="nav-text">1. until语法结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B-2"><span class="nav-number">1.6.</span> <span class="nav-text">2. 应用案例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E6%80%9D%E8%B7%AF"><span class="nav-number">1.6.1.</span> <span class="nav-text">㈡ 思路</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.6.2.</span> <span class="nav-text">㈢ 落地实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%AF%BE%E5%90%8E%E4%BD%9C%E4%B8%9A"><span class="nav-number">2.</span> <span class="nav-text">四、课后作业</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="王增明"
      src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">王增明</p>
  <div class="site-description" itemprop="description">喜欢折腾的极客聚集地.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbnplbmdtaW5n" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wanzengming"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <a href="/wangzengming@aliyun.com" title="E-Mail → wangzengming@aliyun.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王增明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">342k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:11</span>
</div>

        
<div class="busuanzi-count">
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script data-pjax>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


    <div id="pjax">
  

  

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script></body>
</html>
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
